package org.matchit.matcher.collection;

import java.util.Map;
import java.util.Map.Entry;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class EntryKeyMatcher<K> extends TypeSafeMatcher<Map.Entry<K, ?>> {
    private final Matcher<K> keyMatcher;

    public EntryKeyMatcher(Matcher<K> keyMatcher) {
        this.keyMatcher = keyMatcher;
    }

    public static <K> EntryKeyMatcher<K> hasKey(Matcher<K> keyMatcher) {
        return new EntryKeyMatcher<K>(keyMatcher);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean matchesSafely(Entry<K, ?> entry) {
        return keyMatcher.matches(entry.getKey());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void describeTo(Description description) {
        description.appendText("has entry key ").appendDescriptionOf(keyMatcher);
    }

}
